<?php

namespace Library;

use ReflectionClass;
use ReflectionMethod;

class ClassTrigger {
    public static function instantiate($classPath, $className) {
        try {
            require_once ROOT_PATH . "/" . $classPath;
            $reflectionClass = new ReflectionClass($className);
            return $reflectionClass->newInstance();
        } catch (LogicException $Exception) {
            return false;
        } catch (ReflectionException $Exception) {
            return false;
        }

        return false;
    }

    public static function invoke($object, $methodName) {
        return call_user_func(array($object, $methodName));
    }

    public static function invokeArgs($object, $methodName, $args) {
        $reflectionMethod = new ReflectionMethod($object, $methodName); 
        $pass = array();

        foreach ($reflectionMethod->getParameters() as $parameter) {
            if (isset($args[$parameter->getName()])) {
                $pass[] = $args[$parameter->getName()];
            } else
                return false;
        }

        return $reflectionMethod->invokeArgs($object, $pass);
    }
}